import tornado.websocket
import tornado.web
from db import db
from util.myredis import r
conndict={}
class WebsockHandler(tornado.websocket.WebSocketHandler):
    # 跨域支持
    def check_origin(self, origin):
        return True
    #建立连接
    def open(self):
        #获取参数
        id = int(self.get_query_argument("id"))
        #存储链接
        conndict[id]=self
    #接收消息
    def on_message(self,mes):
        print(mes)

    def on_close(self):
        pass


class TTHandler(tornado.web.RequestHandler):
    def get(self):
        msg = self.get_query_argument("mes")
        id = int(self.get_query_argument("id"))
        conndict[id].write_message(msg)
import json,time
class ZZhandler(tornado.web.RequestHandler):
    def get(self):
        while True:
            time.sleep(5)
            id = int(self.get_query_argument("id"))
            wendu = r.list_lpop('key1')
            if wendu:
                # res1 = r.list_lpop('key2')
                # res2 = r.list_lpop('key3')
                print(wendu)
                # print(res1)
                # print(res2)
                sql = "select * from web where id=1"
                res = db.findOne(sql)
                print(res)
                rlist = []
                if int(res['min']) <= int(wendu.decode()) <= int(res['max']):
                    status = 1
                    rlist.append({'id':res['id'],'name':res['name'],'wendu':wendu.decode(),'min':res['min'],'max':res['max'],'status':status})
                else:
                    status = 0
                    rlist.append({'id': res['id'], 'name': res['name'], 'wendu': wendu.decode(), 'min': res['min'], 'max': res['max'],
                                  'status': status})
                conndict[id].write_message(json.dumps(rlist))
            else:
                print('取完了')


